package com.is.export.control;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.is.control.StudentControl;


/**
 * 数据导出类
 * @date 2012-11-12
 * @author Cyoung
 *
 */
public class ExportByReflactor {
	
	public static void ExportStudents(String filename,List ulist,HashMap<String, String> ops,String className)throws IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException{
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("综合数据");
		sheet.setColumnWidth(0, 3800); //設置列寬
		HSSFCellStyle normalStyle = wb.createCellStyle();
		normalStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		normalStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		normalStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		normalStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		normalStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		normalStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		//normalStyle.setWrapText(true);//是否自動換行
		
		//int colIndex = 1;//控制单元格的位置；
		//int colSize = 0;//控制合并单元格的长度
		
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = null;
		int cellNum = 0;
		for(Map.Entry<String, String> mi:ops.entrySet()){
			cell = row.createCell(cellNum++);
			cell.setCellValue(mi.getValue());
			cell.setCellStyle(normalStyle);
		}
		Iterator uit = ulist.iterator();
		int index=1;
		while(uit.hasNext()){
			Class classS = Class.forName(className);
			System.out.println(classS.getName());
//			Method[] methods = classS.getMethods();
			Object object =  Class.forName(className).newInstance();
			object = uit.next();
			int newCell = 0;
			row = sheet.createRow(index++);
			for(Map.Entry<String, String> mi:ops.entrySet()){
				Method method = classS.getMethod(mi.getKey());
				String value=(String) method.invoke(object);
				cell = row.createCell(newCell++);
				cell.setCellValue(value);
				cell.setCellStyle(normalStyle);
			}
		}
		FileOutputStream out = new FileOutputStream(filename);
		wb.write(out);
		out.close();
	}
        	
	/**
	 * @param args
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IOException, ClassNotFoundException, InstantiationException {
//	   Object student = StudentControl.getStudent(15);
//	   HashMap<String, String> ops = SelectOptionValue.StudentOptions();
//	   Class classS = student.getClass();
//	   Method[] method = classS.getMethods();
//	   for(Method m:method){
//	       if(m.getName().indexOf("get")!=-1&&!m.getName().equals("getClass")){
//		       System.out.print(m.getName()+"   ");
//		       Method method2 = classS.getMethod(m.getName());
//		       System.out.println(method2.invoke(student)+"    ");
//	       }
//	   }
//	}
		List a = StudentControl.getStudentByClassId("13");
		HashMap<String, String> ops = SelectOptionValue.StudentOptions();
		ExportStudents("D:\\a.xls", a, ops, "com.is.model.Student");
		System.out.println("FFFF");
	}

}
